package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.service.AccountService;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import com.xmg.p2p.business.service.IMoneyWithdrawService;
import com.xmg.p2p.business.service.IUserBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @author: Andy
 * @date: 2021.4.17 15:16
 * @Description: 前台提现相关控制器
 * @since JDK 1.8
 */
@Controller
public class MoneyWithdrawController {
    @Autowired
    private IUserBankService userBankService;
    @Autowired
    private AccountService accountService ;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private IMoneyWithdrawService moneyWithdrawService ;


    /**
     * 导向到提现申请页面
     * @param model  模型
     * @return  返回申请页面
     */
    @RequestMapping("moneyWithdraw")
    @RequireLogin
    public String moneyWithdraw(Model model){

        Userinfo current = this.userinfoService.getById(UserContext.getCurrent().getId());
        model.addAttribute("haveProcessing",
                current.getHasWithdrawInProcess());  // 判断用户是否已经有一个提现在审核流程中
        model.addAttribute("bankInfo",
                this.userBankService.getBankinfoByUser(UserContext.getCurrent().getId()));  // 通过用户的ID获取银行卡的信息
        model.addAttribute("account",
                this.accountService.getById(UserContext.getCurrent().getId())); // 通过id获取用户信息
        return "moneyWithdraw_apply";
    }

    /**
     * 执行提现申请
     * @param moneyAmount  体现信息
     * @return  JSON字符串
     */
    @RequestMapping("moneyWithdraw_apply")
    @ResponseBody
    public JSONResult apply(BigDecimal moneyAmount)
    {
        JSONResult json = new JSONResult() ;
        try {
            this.moneyWithdrawService.apply(moneyAmount);
        } catch (Exception e) {
            e.printStackTrace() ;
            json.setSuccess(false);
            json.setMsg(e.getMessage());
        }
        return json ;
    }

}
